<?php
namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\facade\Cache;
use app\admin\model\AdminUser;
use app\admin\model\Department as DepartmentModel;
use think\Db;
class Department extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $where = [];
            $page = $this->request->param('page');
            $limit = $this->request->param('limit');
            if($this->request->param('filter')){
                $filter = $this->request->param('filter');
                $filter = json_decode($filter,true);
                foreach($filter as $k=>$row){
                    $where[] = [$k,'like','%'.trim($row).'%'];
                }
            }
            $offset = ($page - 1)*$limit;
            $result = DepartmentModel::where('deleted',0)->where($where)->limit($offset,$limit)->select();
            foreach($result as $k=>$row){
                $result[$k]['admin'] = AdminUser::getAdmin($row['admin_id'],'nickname');
                $result[$k]['user'] = DepartmentModel::getDepartmentUser($row['id']);
                // $user_id = explode(',',$row['user_id']);
                // $user = '';
                // foreach($user_id as $id){
                //     $user .= AdminUser::getAdmin($id,'nickname').',';
                // }
                // $result[$k]['user'] = rtrim($user,',');
            }
            $count = DepartmentModel::where('deleted',0)->where($where)->count();
            $result = array("code" => 0, "count"=>$count,"data" => $result);
            return json($result);
        } else {
            return $this->fetch();
        }
    }

    public function add()
    {
        $adminUser = AdminUser::where(['status'=>1])->select();
        $this->assign('adminUser',$adminUser);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id');
        $department = DepartmentModel::find($id);
        $adminUser = AdminUser::where(['status'=>1])->select();
        $this->assign('adminUser',$adminUser);
        $this->assign('department',$department);
        $user_id = DepartmentModel::getDepartmentUserId($id);
        $this->assign('user_id',$user_id);
        return $this->fetch();

    }

    public function editPost()
    {
        $data = $this->request->param();
        if(!$data['nums']){
            $this->error('请输入部门编号');
        }
        if(!$data['title']){
            $this->error('请输入部门名称');
        }
        $data['admin_id'] = session('admin_user_auth')['uid'];
        $user_id = explode(',',$data['user_id']);
        Db::startTrans();
        try{
            if(!isset($data['id'])){
                $department = DepartmentModel::where(['nums'=>$data['nums'],'deleted'=>0])->find();
                if(!empty($department)){
                    $this->error('部门编号不得重复');
                }
                $data['addtime'] = date('Y-m-d H:i:s');
                $department_id = DepartmentModel::insertGetId($data);
            }else{
                unset($data['nums']);
                $department_id = $data['id'];
                $data['mdtime'] = date('Y-m-d H:i:s');
                DepartmentModel::where('id',$data['id'])->update($data);
            }
            Db::name('gd_department_user')->where(['department_id'=>$department_id])->delete();
            foreach($user_id as $k=>$id){
                Db::name('gd_department_user')->insertGetId([
                    'user_id'=>$id,
                    'department_id'=>$department_id,
                    'addtime'=>date('Y-m-d H:i:s')
                ]);
            }
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            $this->error('编辑失败');
        }
        $this->success('添加成功');
    }

    public function delete()
    {
        $data = $this->request->param();
        DepartmentModel::where('id',$data['id'])->update(['deleted'=>1]);
        $this->success('删除成功',url('index'));
    }

    
}